package com.yoomiito.app.widget.songduoduo

import android.content.Context
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.model.sdd.SddGoodsInfo
import com.yoomiito.app.model.sdd.SddFreeGoodsList
import com.yoomiito.app.ui.songduoduo.freegoods.FreeGoodsListActivity
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.RouterUtils
import com.yoomiito.app.utils.Type
import com.yoomiito.app.widget.SpacingDecoration

/**
 * 送多多---赠品专区
 */
class SendMoreFreeGoodsZoneView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    :LinearLayout(context, attrs, defStyleAttr, defStyleRes){

    private val adapter1 = Adapter()
    private val adapter2 = Adapter()

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_send_more_free_goods_zoon,null,false)
        val lp = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
        addView(view,lp)
        view.findViewById<ImageView>(R.id.more).setOnClickListener { FreeGoodsListActivity.start(context) }
        view.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.goodsRcy1).let {
            it.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context, HORIZONTAL, false)
            it.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(3f), 0, false))
            it.adapter =adapter1
            adapter1.setOnItemClickListener { adapter, view, position ->
                RouterUtils.toGoodsDetail(context,adapter1.data[position].id,Type.GoodsType.FreeGift)
            }
        }
        view.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.goodsRcy2).let {
            it.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context, HORIZONTAL, false)
            it.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(3f), 0, false))
            it.adapter =adapter2
            adapter2.setOnItemClickListener { adapter, view, position ->
                RouterUtils.toGoodsDetail(context,adapter2.data[position].id,Type.GoodsType.FreeGift)
            }
        }
    }

    fun setGoodsData(freeGoodsList: SddFreeGoodsList){
        adapter1.setNewData(freeGoodsList?.one)
        adapter2.setNewData(freeGoodsList?.two)
    }

    inner class Adapter:BaseQuickAdapter<SddGoodsInfo,BaseViewHolder>(R.layout.item_send_more_free_goods,null){

        override fun convert(helper: BaseViewHolder?, item: SddGoodsInfo?) {
            helper?.getView<androidx.cardview.widget.CardView>(R.id.freeGoods).let {
                val lp = it?.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
                lp.leftMargin = DensityUtil.dp2px( if (helper?.layoutPosition==0)12f else 0f)
            }
            helper?.getView<ImageView>(R.id.goodsPic).let {
//                it?.setImageResource(R.drawable.app_launcher)
                ImageLoader.getInstance().into(context,item?.productImg,it)
            }
            helper?.setText(R.id.label,"${item?.volume}人已领取")
        }
    }
}